package xyz.erupt.flow.core.engine.formItems.Impl;

import com.alibaba.fastjson.JSONObject;
import org.camunda.bpm.model.bpmn.instance.ExtensionElements;
import org.camunda.bpm.model.bpmn.instance.UserTask;
import org.camunda.bpm.model.bpmn.instance.camunda.CamundaFormData;
import org.camunda.bpm.model.bpmn.instance.camunda.CamundaFormField;
import org.camunda.bpm.model.bpmn.instance.camunda.CamundaValue;
import org.camunda.bpm.model.xml.ModelInstance;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import xyz.erupt.flow.bean.entity.OaWorkFlowDefinition;
import xyz.erupt.flow.bean.entity.form.FormComponent;
import xyz.erupt.flow.core.engine.Utils.BeanUtil;
import xyz.erupt.flow.core.engine.enums.FORM_ITEM_TYPE;
import xyz.erupt.flow.core.engine.formItems.WorkFlowFormField;
import xyz.erupt.flow.core.engine.formItems.WorkFlowFormItemService;
import xyz.erupt.flow.core.engine.formItems.vo.FormItemFieldVo;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 表单字段实现
 */
@Service
public class WorkFlowFormFieldImpl implements WorkFlowFormField {

    @Autowired
    private BeanUtil beanUtil;

    /**
     * 创建表单变量
     * @param modelInstance
     * @param formComponent
     * @return
     */
    @Deprecated
    private List<CamundaFormField> CreateFormField(ModelInstance modelInstance, FormComponent formComponent) {
        ArrayList<CamundaFormField> camundaFormFields = new ArrayList<>();
        WorkFlowFormItemService formItemService = beanUtil.getBeanByName(formComponent.getName(), WorkFlowFormItemService.class);//获取formItem处理类
        List<FormItemFieldVo> formItemFieldVos = formItemService.generateFormItemFieldVoList(formComponent);

        if (formItemFieldVos!=null&& formItemFieldVos.size() > 0) {
            for (FormItemFieldVo formItemFieldVo : formItemFieldVos) {
                //创建表单字段
                CamundaFormField formField = modelInstance.newInstance(CamundaFormField.class);
                formField.setCamundaId(formItemFieldVo.getId());
                formField.setCamundaLabel(formItemFieldVo.getLabel());
                formField.setCamundaType(formItemFieldVo.getType().getCode());
                formField.setCamundaDefaultValue(formItemFieldVo.getDefaultValue());
                if (formItemFieldVo.getType().equals(FORM_ITEM_TYPE.ENUM)){//枚举类型需添加values
                    if (!formItemFieldVo.getEnumValues().isEmpty()){
                        for (String s : formItemFieldVo.getEnumValues().keySet()) {
                            CamundaValue camundaValue = modelInstance.newInstance(CamundaValue.class);
                            camundaValue.setCamundaId(s);
                            camundaValue.setCamundaName(formItemFieldVo.getEnumValues().get(s));
                            formField.getCamundaValues().add(camundaValue);
                        }
                    }

                }
                camundaFormFields.add(formField);
            }
        }

        return camundaFormFields;
    }


    /**
     * 为用户任务添加表单变量
     * @param modelInstance
     * @param userTask
     * @param flowDefinition
     */
    @Deprecated
    @Override
    public void AddFormFields(ModelInstance modelInstance, UserTask userTask, OaWorkFlowDefinition flowDefinition) {
        List<FormComponent> formItemsList = flowDefinition.getFormItemsList();
        ArrayList<CamundaFormField> camundaFormFields = new ArrayList<>();
        if(!formItemsList.isEmpty()){
            for (FormComponent formItem : formItemsList) {
                List<CamundaFormField> camundaFormFields1 = CreateFormField(modelInstance, formItem);
                camundaFormFields.addAll(camundaFormFields1);
            }
        }
        //创建表单字段
        if(!camundaFormFields.isEmpty()){
            CamundaFormData camundaFormData = modelInstance.newInstance(CamundaFormData.class);
            for (CamundaFormField camundaFormField : camundaFormFields) {
                camundaFormData.addChildElement(camundaFormField);
            }
            ExtensionElements extensionElements = modelInstance.newInstance(ExtensionElements.class);
            extensionElements.addChildElement(camundaFormData);
            userTask.setExtensionElements(extensionElements);
        }

    }

}
